package com.hjxr.quota.define.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjxr.quota.define.domain.DimRelation;
import com.hjxr.quota.define.domain.vo.IdNameVo;
import com.hjxr.quota.define.mapper.DimRelationMapper;
import com.hjxr.quota.datasource.domain.QuotaDimension;
import com.hjxr.quota.define.service.DimRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ma
 * @version v1.0
 * @date 2020/9/17
 **/
@Service
public class DimRelationServiceImpl extends ServiceImpl<DimRelationMapper, DimRelation> implements DimRelationService {
    @Resource
    private DimRelationMapper dimRelationMapper;

    @Override
    public Boolean insert(Map<Integer, List<IdNameVo>> parms) {
        DimRelation dimRelation =new DimRelation();
        try {
            for(Map.Entry<Integer, List<IdNameVo>> entry : parms.entrySet()){
                Integer quotaId = entry.getKey();
                List<IdNameVo> dimIds = entry.getValue();
                for (IdNameVo idNameVo:dimIds) {
                    dimRelation.setQuotaId(quotaId);
                    dimRelation.setDimensionId(idNameVo.getId());
                    save(dimRelation);
                }
            }
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 根据指标id查询维度信息
     * @param id
     * @return
     */
    @Override
    public List<QuotaDimension> queryDim(Integer id) {
        //判断此指标id是否有对应维度id
        QueryWrapper<DimRelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("QUOTA_ID",id);
        if(dimRelationMapper.selectList(queryWrapper).size()==0){
            return null;
        }
        return dimRelationMapper.queryDim(id);
    }
}
